#include <bits/stdc++.h>

using namespace std;

class tree_node {
    public:
        tree_node(int val = -1, tree_node* left = NULL, tree_node* right = NULL, bool is_leaf = true) 
            :m_right(right), m_left(left), m_is_leaf(is_leaf), m_val(val) {}
        tree_node* m_left;
        tree_node* m_right;
        int m_val;
        bool m_is_leaf;
};

void last_order(tree_node* tree, string& output) {
    if (tree->m_is_leaf) {
        return;
    }
    // if (tree->m_left == NULL && (tree->m_right == NULL)) {
    //     return;
    // }
    last_order(tree->m_left, output);
    if (tree->m_right != NULL) {
        last_order(tree->m_right, output);
    }
    output = output + to_string(tree->m_val) + " ";
}

int main() {
    // parse bin tree
    int tmp;
    // tree_node* root = new tree_node;
    queue<tree_node*> node_queue;
    cin >> tmp;
    tree_node* root = new tree_node(tmp);
    node_queue.push(root);
    while (cin.peek() != '\n') {
        tree_node* cur_node =  node_queue.front();
        cur_node->m_is_leaf = false;
        cin >> tmp;
        tree_node* left_node = new tree_node(tmp);  // left node
        cur_node->m_left  = left_node;
        node_queue.push(left_node);
        if (cin.peek() != '\n') {
            cin >> tmp;
            tree_node* right_node = new tree_node(tmp);  // right node
            cur_node->m_right  = right_node;
            node_queue.push(right_node);
        }
        node_queue.pop();
    }

    // last order
    string output;
    last_order(root, output);
    output.pop_back();
    cout << output;
}